define([
	'jquery',
	'underscore',
	'backbone',
	'catjs/util',
	],function($,_,Backbone,util){

	var module = {};

	var WIDTH = window.innerWidth;
	var HEIGHT = window.innerHeight;

	//教学楼视图
	var TeachView = Backbone.View.extend({

		el : $('#teach-building'),
		mid : $('#teach-building .class-mid-floor'),
		right : $('#teach-building .class-right-floor'),
		all : $('#teach-building .class-right-floor,#teach-building .class-mid-floor'),
		events : {
			//中间房间
			'click .class-mid-down' : 'leave',
			'click .class-mid-right' : 'toRgith',
			//右边房间
			'click .class-right-left' : 'right2mid',
			'click .class-right-post' : 'addMessage',
		},

		init : function(){
			var self = this;
			self.mid.css('left',0);
			self.right.css('left',+WIDTH);
		},
		render : function(){
			var self = this;
			self.init();
			//课程信息列表
			$.ajax({
				url : '/Teach/GetClassList',
				type : 'GET',
				data : {
					buildingId : self.teach_id,
				},
				success : function(json){
					//排序
					var NUM_MAP = ['','一','二','三','四','五','六','七'];
					var floors = JSON.parse(json);
					floors.sort(function(a,b){
						return a.floorNumber - b.floorNumber;	
					});
					floors.forEach(function(floor){
						floor.name = NUM_MAP[floor.floorNumber];
						floor.roomList.sort(function(a1,b1){
							return a1 - b1;
						});
					});
					self.$el.find('.class-mid-textbox').html(
						util.ejs.render($('#tplClassMidDiv').text(),{floors:floors})
					);
					console.log(floors);
					self.$el.fadeIn();
				},
			});
		},
		//移动
		toRgith : function(){
			var self = this;
			//留言列表
			$.ajax({
				url : '/Teach/GetMessageList',
				type : 'GET',
				data : {
					buildingId : self.teach_id,
				},
				success : function(json){
					var messages = JSON.parse(json);
					self.$el.find('.class-right-ul').html(
						util.ejs.render($('#tplClassRightLi').text(),{messages:messages})
					);
					console.log(messages);
					self.scrollRight();
				},
			});
		},
		right2mid : function(){
			var self = this;
			self.scrollLeft();
		},
		//向左边移动
		scrollLeft : function(){
			var self = this;
			self.all.animate({
				'left' : '+='+WIDTH,
			},1000,'swing');
		},
		//向右边移动
		scrollRight : function(){
			var self = this;
			self.all.animate({
				'left' : '-='+WIDTH,
			},1000,'swing');
		},
		//增加留言
		addMessage : function(){
			var self = this;
			$('#dialog2 .dialog1-wrap').css({
				'width' : 450,
				'top' : 100,
			});
			$('#dialog2 .dialog1-hd').html('留言');
			$('#dialog2 .dialog1-bd').html($('#tplMessage').text());
			$('#dialog2 .message').click(function(){
				$.ajax({
					url : '/Teach/AddMessage',
					type : 'POST',
					data : {
						buildingId : self.teach_id,
						userId : util.getUser().userId,
						contents : $('#dialog2 .message-content').val(),
					},
					success : function(json){
						var message = JSON.parse(json);
						console.log(message);
						self.$el.find('.class-right-ul').prepend(
							util.ejs.render($('#tplClassRightLi').text(),{messages:[message]})
						);
						$('#dialog2').fadeOut();
					},
				});
			});
			$('#dialog2').fadeIn();
		},
		//离开教学楼
		leave : function(){
			var self = this;
			self.$el.fadeOut();
		},
	});
	var teach = new TeachView;

	module.enter = function(teach_id){
		teach.teach_id = teach_id;
		teach.render();
	};
	
	return module;
});